<script type="text/javascript">
$(function(){
	$.post("roleManage/getRoleByPersonalSet",function(json){
		$(json).each(function(n,item){	
			$("input[name='roleId']").val(item.roleId);
			$("input[name='roleName']").val(item.roleName);
			/* $("input[name='roleNo']").val(item.roleNo);*/
			$("input[name='sysId']").val(item.sysId); 
		})
	},"json");
	$.post("roleManage/allPermission",allPermission,"json");
	//ckeckTree(menu);
});
var hasPermission=new Array();
//获取所有权限
 //var ids,menu,childMenus,grandMenus, greatGrandMenus; 
function allPermission(data){
	if(data.statusCode==DWZ.statusCode.error){
		navTabAjaxDone(data);
		return
	}
	 var ids, menu,childMenus,grandMenus, greatGrandMenus;
	 ids=data.mVos;
	 hasPermission=data.mVos;
	var json=data.meaus;
	menu=json;
	var menus="<ul  class='tree treeFolder treeCheck expand' oncheck='setPermission'>";
	for(var i=0;i<json.length;i++){
		menus+="<li><a tname='frist' tvalue="+json[i].id+">"+json[i].privilegeTypeName+"</a>";
		childMenus=json[i].childMenus;
		 if(childMenus!=null){
			menus+="<ul>";
			for(var j=0;j<childMenus.length;j++){
				menus+="<li><a tname='' tvalue="+childMenus[j].id+">"+childMenus[j].privilegeTypeName+"</a>";
				grandMenus=childMenus[j].childMenus;
				if(grandMenus!=null){
					menus+="<ul>";
					for(var k=0;k<grandMenus.length;k++){
						menus+="<li><a tname='' tvalue="+grandMenus[k].id+">"+grandMenus[k].privilegeTypeName+"</a>";
						greatGrandMenus=grandMenus[k].childMenus;
						if(greatGrandMenus!=null){
							menus+="<ul>";
							for(var m=0;m<greatGrandMenus.length;m++){
								menus+="<li><a tname="+greatGrandMenus[m].id+" tvalue="+greatGrandMenus[m].id+">"+greatGrandMenus[m].privilegeTypeName+"</a></li>";
							}
							menus+="</ul>";
						} 
						menus+="</li>";
					}
					menus+="</ul>";
				} 
				menus+="</li>";
			}
			menus+="</ul>";
		} 
		menus+="</li></ul>";
	}
	$("#treePermission").html($(menus).jTree());//将ul格式改成Tree
	
	//获取某个角色已有哪些权限
/* 	$("div.ckbox").find("input").each(function(){
		var v=this.value;
		for(var i=0;i<ids.length;i++){
			if(v==ids[i]){
				$(this).attr("checked","checked");
				$(this).parent().addClass("checked");
				return
				/* alert($(this).parent().parent().html());
				$(this).parent().parent().parents().find(".ckbox").addClass("indeterminate"); */
/*			}
		}		
	}); */
	//var ids, menu,childMenus,grandMenus, greatGrandMenus;
	//$("input[name='1502']").attr("checked","checked");
	//$("input[name='1502']").parent().addClass("checked");
	addChecked4(ids,menu)
}
function addChecked4(ids,menu){
	var num=0;
	for(var i=0;i<menu.length;i++){
		console.log("addChecked4==="+menu[i].childMenus.length)
		var f=addChecked3(ids,menu[i].childMenus)
		//alert("addChecked4---f"+f)
		//alert("menu[i].id="+menu[i].id)
		 if(typeof(f)=="string"){
			continue;
		 }
		 if(f){
				num++;
				$("input[value="+menu[i].id+"]").attr("checked","checked");
				$("input[value="+menu[i].id+"]").parent().addClass("checked");
			}else if(!f){
				//$("input[name="+grandMenus[i].id+"]").attr("checked",true);
				$("input[value="+menu[i].id+"]").parent().addClass("indeterminate");
			}
		}	
}
function addChecked3(ids,childMenus){
	var ff=true;
	var num=0;
	for(var i=0;i<childMenus.length;i++){
		//alert("childMenus[i].id="+childMenus[i].id)
		console.log("addChecked3==="+childMenus[i].childMenus.length)
		var f=addChecked2(ids,childMenus[i].childMenus);
		//alert("typeof f"+typeof(f))
		//alert("childMenus[i].id="+childMenus[i].id+"，返回值："+f)
		 if(typeof(f)=="string"){
			//alert("进来了0--"+f)
			continue;
		 }
		 if(f){
			//alert("进来了true--"+f)
			num++;
			$("input[value="+childMenus[i].id+"]").attr("checked","checked");
			$("input[value="+childMenus[i].id+"]").parent().addClass("checked");
		}if(f==false){
			//alert("进来了false--"+f)
			$("input[value="+childMenus[i].id+"]").parent().addClass("indeterminate");
			ff=false;
		}
	}
	//alert("num="+num+"====childMenus.length="+childMenus.length)
	
	if(num==childMenus.length){
		return true;
	}else if(num>0&&num<childMenus.length||!ff){
		return false;
	}else{
		return "0";
	}	
}

function addChecked2(ids,grandMenus){
	var ff=true;
	var num=0;
	for(var i=0;i<grandMenus.length;i++){
		//alert("grandMenus[i].id="+grandMenus[i].id)
		//alert("grandMenus[i].childMenus.length="+grandMenus[i].childMenus.length)
		console.log("addChecked2==="+grandMenus[i].childMenus.length)
		var f=addChecked(ids,grandMenus[i].childMenus)
			if(typeof(f)=="string"){
				continue;
			}
			if(f){
				num++;
				$("input[value="+grandMenus[i].id+"]").attr("checked","checked");
				$("input[value="+grandMenus[i].id+"]").parent().addClass("checked");
			}else if(!f){
				//$("input[name="+grandMenus[i].id+"]").attr("checked",true);
				$("input[value="+grandMenus[i].id+"]").parent().addClass("indeterminate");
				ff=false;
			}
		}
	
	//alert("num="+num+"====grandMenus.length="+grandMenus.length)
	if(num==grandMenus.length){
		return true;
	}else if(num<grandMenus.length&&num>0||!ff){
		return false;
	}else{
		return "0";
	}
}

function addChecked(ids,greatGrandMenus){
	var num=0;
	console.log("addChecked==>"+greatGrandMenus.length);
	for(var i=0;i<greatGrandMenus.length;i++){
		for(var j=0;j<ids.length;j++){
			if(greatGrandMenus[i].id==ids[j]){
				num++;
				//alert("ids[j]="+ids[j]+";;;menu[i]="+menu[i].id)
				$("input[name="+greatGrandMenus[i].id+"]").attr("checked","checked");
				$("input[name="+greatGrandMenus[i].id+"]").parent().addClass("checked");
			}
		}
	}
	//alert("num="+num+"====greatGrandMenus.length="+greatGrandMenus.length)
	if(num!=0){
		if(num==greatGrandMenus.length){
			return true;
		}else{
			return false;
		}
	}else{
		return "0";
	}
	
	
}
		
//**************给角色设置权限********************************************************************
function setPermission(){
	var json = arguments[0], result="";
	var roleId=$("input[name='roleId']").val();
	var sysId=$("input[name='sysId']").val();
	var frist=$("input[name='frist']").val();
	var sign;
	var permissions=new Array();
	
	//alert(json.checked);
	
	alertMsg.confirm("是否确定", {
			okCall: function(){
		
	if(json.checked){
		$(json.items).each(function(i){
			result = "<p>name:"+this.name + " value:"+this.value+" text: "+this.text+"</p>";
			if(this.name!=undefined&&this.value!=frist){
				//alert(result+"************"+i);
				var res=$.inArray(parseInt(this.value),hasPermission);
				if(res==-1){
					permissions.push(this.value);
				}
			}
		});
		sign=true;//选中标识
	}else{
		$(json.items).each(function(i){
			result = "<p>name:"+this.name + " value:"+this.value+" text: "+this.text+"</p>";
			if(this.name!=undefined&&this.value!=frist){
				//alert(result+"************"+i);
				permissions.push(this.value);
			}
		});
		sign=false;//取消标识
	}
	
	$.ajax({
		url:"roleManage/setPermission",
		 dataType:'json',
		 type:"post",
	 	 traditional:true,
	 	 data:{
	 		 "sysId":sysId,
	 		 "roleId":roleId,
	 		 "sign":sign,
	 		 "permissions":permissions,
	 	 },
	 	success:function(json){
	 		if(json.statusCode="200"){
	 			alertMsg.correct("权限添加成功");
	 		}
		}
	})
},
	 cancelCall: function(){
		 $.post("roleManage/allPermission",allPermission,"json"); 
	 }
})

	/* $("#resultBox").html(result); */
	
}
</script>
<!-- <div id="resultBox"></div> -->
<div class="pageContent">
	<form method="post" action="roleManage/" class="pageForm required-validate" onsubmit="return validateCallback(this,dialogAjaxDone);">
		<div class="pageFormContent" layoutH="18" hight="500px">
			<dl>
				<dt>角色名称：</dt>
				<dd>
					<input type="text" name="roleName" class="required" size="30" />
				</dd>
			</dl>
			<input type="hidden" name="roleId"  size="30" />
			<input type="hidden" name="sysId"  size="30" />
			<div class="divider"></div>
			<div layoutH="8" style=" float:left; display:block; margin:10px; overflow:auto; width:610px; height:500px; overflow:auto; border:solid 1px #CCC; line-height:21px; background:#FFF; margin-left: 10px">
			<p>角色权限设置</p>
			
			 <div id="treePermission" ></div>
			
			</div>	
		</div>		
		
	</form>
</div>